import { Animation, AnimationSystem, TranslateAnimation } from "@nirvana/animation";
import { AnimationListener } from "@nirvana/animation";
import { WebGLCanvas } from "@nirvana/canvas";
import { Camera, SceneNode } from "@nirvana/core";

async function main() {
  let canvas = document.getElementById("engine-nirvana") as HTMLCanvasElement;
  const webCanvas = new WebGLCanvas(canvas, {});
  const engine = webCanvas.engine;
  const scene = engine.sceneManager.createScene("root");

  const camera = new Camera("root camera", engine);
  scene.addCamera(camera);
  camera.transform.setPosition(0, 0, 10);

  const gl = engine.gl;

  const entity = new SceneNode("e");
  const asys = new AnimationSystem("a");
  const animation = asys.addAnimation("Translate");
  animation.setRepeatCount(0);
  animation.setDuration(20000);
  // animation._duration = 20000;
  // animation._repeatCount = 1;
  // animation._startOffset = 1000;
  // animation._repeatMode = "reverse";
  // animation._fillBefore = false;
  // animation._fillAfter = true;
  // animation.applyTransformation = (interpolatedTime) => {
  //   // console.log('...', interpolatedTime * 2 - 1, interpolatedTime * 2 - 1)
  //   // obj.setPosition(interpolatedTime * 2 - 1, interpolatedTime * 2 - 1, 0)
  //   // obj.setPosition(0, 0, interpolatedTime * 2 - 1)
  //   // box3.followAt(obj)
  //   // console.log("--==");
  // };

  const r = new AnimationListener();
  animation.setAnimationListener(r);
  r.registerStartListener((ani) => {
    console.log("start...");
  })
    .registerEndListener((ani) => {
      console.log("end...", ani._oneMoreTime, ani._started, ani._ended, ani._more);
    })
    .registerRepeatListener((ani) => {
      console.log("repeat...", ani._repeated);
    })
    .registerProgressListener((ani, progress) => {
      console.log("animation", progress);
      // ball.setPosition(0, 0, (progress * 2 - 1.0) * 10);
      // box3.followAt(ball)
      // camera1.followAt(ball, 10)
    });

  asys.startAnimation(animation);
  scene.addAnimationSystem(asys);
  //

  engine.update = () => {
    // animation.updateAnimation();
    // ani.updateAnimation();
  };
}

main();
